import {createApp} from 'vue'
import App from './App.vue'

// Vuetify
import 'vuetify/styles' //'vuetify/styles' should not be used in sass files as it resolves to precompiled css
import {createVuetify} from 'vuetify'
import { pl, zhHans } from 'vuetify/locale'
import * as directives from 'vuetify/directives'
import '@mdi/font/css/materialdesignicons.css'
import * as components from 'vuetify/components'
import * as labsComponents from 'vuetify/labs/components'
import ElementPlus from 'element-plus'
import Antd from 'ant-design-vue';
import 'element-plus/dist/index.css'
import 'ant-design-vue/dist/reset.css';
import { message } from "ant-design-vue"

message.config({
    top: "400px"
})
// Router
import {createRouter, createWebHashHistory} from 'vue-router'
import Login from "./Login.vue";
import Backend from "./Backend.vue";
import Users from "./Users.vue";
import Role from "./role.vue";
import Menu from "./menu.vue";
import Mobile from "./Mobile.vue";
import Cockpit from "./Cockpit.vue";
import NotFound from "./notFound.vue"
import Log from "./log.vue";
import MedicineBaseInfo from "./medicineBaseInfo.vue";
import MedicinePointBaseInfo from "./medicinePointBaseInfo.vue";
import MedicineHistory from "./medicineHistory.vue";
import MedicinePointHistory from "./medicinePointHistory.vue";
import MedicineOrder from "./medicineOrder.vue";
import MedicineOrderRecord from "./medicineOrderRecord.vue";
import MedicinePointCalc from "./medicinePointCalc.vue";
import MedicineOrderRegister from "./medicineOrderRegister.vue";
import MedicineSupplierRegister from "./medicineSupplierRegister.vue";
import MedicineSupplierRecord from "./medicineSupplierRecord.vue";
import MedicineRegisterRecordOrder from "./medicineRegisterRecordOrder.vue";
import MedicineOrderReport from "./medicineOrderReport.vue";
import MedicinePointReport from "./MedicinePointReport.vue"
import "./index.css"

// Axios
import axios from 'axios'
import {snackbar} from "./store";

const vuetify = createVuetify({
    directives,
    components: {
        ...components,
        ...labsComponents,

    },
    locale: {
        locale: 'zhHans'
      },
    theme: {
        themes: {
            light: {
                dark: false,
                colors: {
                    primary: '#0071EB',
                    background: '#f5f5f7'
                }
            },
        },
    }
})

export const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/backend',
            component: Backend,
            redirect: '/users',
            children: [
                {path: '/user', component: Users},
                {path: '/role', component: Role},
                {path: '/menu', component: Menu},
                {path: '/log', component: Log},
                {path: '/medicine_base_info', component: MedicineBaseInfo},
                {path: '/medicine_point_base_info', component: MedicinePointBaseInfo},
                {path: '/medicine_history', component: MedicineHistory},
                {path: '/medicine_point_history', component: MedicinePointHistory},
                {path: '/medicine_order', component: MedicineOrder},
                {path: '/medicine_order_record', component: MedicineOrderRecord},
                {path: '/medicine_point_calc', component: MedicinePointCalc},
                {path: '/medicine_order_register', component: MedicineOrderRegister},
                {path: '/medicine_supplier_register', component: MedicineSupplierRegister},
                {path: '/medicine_supplier_record', component: MedicineSupplierRecord},
                {path: '/medicine_register_record_order', component: MedicineRegisterRecordOrder},
                {path: '/medicine_order_report', component: MedicineOrderReport},
                {path: '/medicine_point_report', component: MedicinePointReport}
            ]
        },
        {
            path: '/mobile',
            component: Mobile
        },
        {
            path: '/cockpit',
            component: Cockpit
        },
        {path: '/login', component: Login},
        {path: '/:pathMatch(.*)*', component: NotFound}
    ]
})
// Axios
//第一医院224
//中医院172.168.180.4:8092
// axios.defaults.baseURL = 'http://172.168.180.4:8092/smartHospital';
axios.defaults.baseURL = 'http://101.68.222.195:6442';
// 第二医院21
// axios.defaults.baseURL = 'http://192.192.41.21:8092/smartHospital';
// axios.defaults.baseURL = 'http://101.68.222.195:8092/smartHospital';
// axios.defaults.baseURL = 'https://api.hdingding.jxtvtech.com/smartHospital/';
axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.put['Content-Type'] = 'application/json';
axios.interceptors.request.use(config => {
    const token = localStorage.getItem('token');
    if (token) {
        config.headers['token'] = token;
    }
    return config;
})

axios.interceptors.response.use(res => {
    switch (res.data.code) {
        case 0:
            return res;
        case 998:
            snackbar.info(res.data.message)
            localStorage.removeItem('token')
            router.push('/login')
        break
        case 999:
            snackbar.info(res.data.message)
            localStorage.removeItem('token')
            router.push('/login')
        break
        default:
            // snackbar.info(res.data.message)
            return Promise.reject(res);
    }
}, err => {
    if (err.data) {
        switch (err.data.status) {
            case 401:
                snackbar.info('认证失败,请重新登录')
                localStorage.removeItem('token')
                router.push('/login')
                break
            case 403:
                snackbar.info('无权限')
                break
            case 500:
                break
            default:
                break                
        }
    }
    return Promise.reject(err);
})

createApp(App).use(Antd).use(ElementPlus).use(router).use(vuetify).mount('#app')

